#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#define  BUFSIZE            (100)
#define  ADRESS             "127.0.0.2"
#define  PORT               (9734)
#define  OUT
#define  ARGUM               (3)


static void* client3(void* arg);

char** argv;

static int SocketNew(int* _socket,struct sockaddr_in* _saddr,const char* _name)
{
    *_socket = socket( AF_INET /*1st argument is the purpose AF_UNIX local*/ ,
                SOCK_STREAM /*communication sematics SOCK_STREAM ==two way connect*/,
                0/*Protocol*/);

    if(*_socket ==-1)
    {
        perror("socket");
        return 1;
    }
    _saddr->sin_family = AF_INET;/*dueto AF_UNIX unix(7) instructions*/
    _saddr->sin_addr.s_addr = inet_addr(ADRESS);/*dueto AF_UNIX unix(7) instructions)*/
    _saddr->sin_port = htons(PORT);/*convert byte representation on number to network represent*/
    return 0;
}

/*Add to the massage Header at sizeof int + 1 and send to server*/
static int AddHeader(char* buf1,char* buf2)
{
    *(int*)buf2 = strlen(buf1)+1;
    strcat((buf2 + sizeof(int)),buf1);
    return  *(int*)buf2 + sizeof(int);
}

int main(int _argc,char** _argv)
{

    pthread_t           thread[ARGUM ];

    argv = _argv;

    pthread_create( &thread[0], NULL, client3, 0);
/*    pthread_create( &thread[1], NULL, client2, 0);*/

    pthread_join(thread[0], NULL);
  return 0;
}

static void* client3(void* arg)
{
    int                 client_sockfd;
    struct sockaddr_in  client_saddr;
    char                recievbuf[100];
    int                 err;
    int                 wr;
    FILE*               fp;

    char*               mes3="Client 3 Waiting for message   : ";
    int                 ln;
    char                headbuffer[BUFSIZE];
    /*remove(argv[3]);*/

    if(SocketNew(&client_sockfd,&client_saddr,0) )
    {
        fprintf(stderr,"\nSocket New problems");
        exit(EXIT_FAILURE);
    }

    err = connect(client_sockfd,(struct sockaddr*)&client_saddr,sizeof(client_saddr));
    if(err == -1)
    {
        perror("connect");
        fflush(stdout);
        exit(EXIT_FAILURE);
    }
/*    close(err);*/

    mknod(argv[3],S_IFIFO | 0666,0);
    fp = fopen(argv[3],"w");
    fprintf( fp,"%s",argv[3]);
    fclose(fp);
    printf("\n Client 3 try to connect... \n");
    while(1)
    {
        memset(headbuffer,0,sizeof(headbuffer));
        ln=AddHeader(mes3,OUT headbuffer);
        wr= send(client_sockfd,headbuffer,ln,MSG_DONTWAIT);

        if(wr == -1 && errno == EPIPE)
        {
            perror(" mes3 send :    ");
            break;
        }

        wr = recv(client_sockfd,recievbuf,100,MSG_DONTWAIT);

        printf("\n Recieve message  : \" %s \" \n",recievbuf);
        recievbuf[0]=0;
        if(wr == -1 && errno == EPIPE)
        {
            break;
        }
    }
  close(client_sockfd);
  remove(argv[3]);
  printf("\nClient 3 finished\n");
  return (void*) 0;
}


